package com.ninong.ker.admin.rest;

import java.util.List;

import com.ninong.ker.common.tools.PropertyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import com.ninong.ker.admin.domain.Menu;
import com.ninong.ker.admin.sevice.MenuService;
import com.ninong.ker.common.annotation.log.Log;
import com.ninong.ker.common.annotation.perm.CmdAdmin;
import com.ninong.ker.common.response.CmdResponse;

/**
 * 菜单资源
 *
 * @author cmd
 */
@RestController
@RequestMapping("/cmd/v1/menu")
public class MenuResources {

    @Autowired
    private MenuService menuService;

    @Log("菜单列表")
    @CmdAdmin({"admin", "menu:list","role:list"})
    @PostMapping
    public ResponseEntity<CmdResponse> list(@RequestBody Menu dto) {
        CmdResponse response = new CmdResponse();
        response.setData(menuService.listTree(dto));
        return ResponseEntity.ok(response);
    }

    /**
     * 可搜索
     * @param dto
     * @return
     */
    @Log("菜单列表")
    @CmdAdmin({"admin", "menu:list","role:list"})
    @PostMapping("menus")
    public ResponseEntity<CmdResponse> menus(@RequestBody Menu dto) {
        CmdResponse response = new CmdResponse();
        response.setData(menuService.listMenusTree(dto));
        return ResponseEntity.ok(response);
    }

    @Log("菜单删除")
    @CmdAdmin({"admin", "menu:delete"})
    @DeleteMapping
    public ResponseEntity<CmdResponse> delete(@RequestBody String[] ids) {
        CmdResponse response = new CmdResponse();
        menuService.delete(ids);
        String mess = PropertyUtil.getProperty("DELETE_SUCCESS");
        response.setMessage(mess);
        return ResponseEntity.ok(response);
    }

    @Log("菜单新增或编辑")
    @CmdAdmin({"admin", "menu:add", "menu:update"})
    @PutMapping
    public ResponseEntity<CmdResponse> create(@RequestBody Menu entity) {
        menuService.create(entity);
        return ResponseEntity.ok(new CmdResponse());
    }

    @Log("菜单详情")
    @GetMapping(value = "/{id}")
    public ResponseEntity<CmdResponse> get(@PathVariable String id) {
        CmdResponse response = new CmdResponse();
        response.setData(menuService.get(id));
        return ResponseEntity.ok(response);
    }

    @Log("获取已选中数据")
    @GetMapping(value = "/tree/{roleId}")
    public ResponseEntity<CmdResponse> list(@PathVariable String roleId) {
        CmdResponse response = new CmdResponse();
        response.setData(menuService.listTreeByRoleId(roleId));
        return ResponseEntity.ok(response);
    }

    @Log("用色菜单关系更新")
    @PostMapping(value = "/merge/{roleId}")
    public ResponseEntity<CmdResponse> updateRoleMenuByRoleId(@PathVariable String roleId,
                                                              @RequestBody List<String> menuIds) {
        menuService.updateRoleMenuByRoleId(roleId, menuIds);
        return ResponseEntity.ok(new CmdResponse());
    }
}
